
var noLoginName = "用户昵称";
var tabbarJs = require("../tabbar/tabbar.js");
var init = tabbarJs.init;
var onTabbarItemTap = tabbarJs.onTabbarItemTap;
var app = getApp();
var session = require('../../js/session');
var requestMode = require('../../js/requestsMode.js');
var Tools = require('../../js/Tools.js');
const PageData = {
    globalData: {
        userInfo: null
    },
    data: {
        jhDataForTabbar :Tools.tabBarItems(4,0),
        userInfo : {
            avatar: "",
            nickName : noLoginName,
            detailInfo:""
        },
        bottomList : [
            {
                image: "../../images/suggest.png",
                listItem_id:"listItem_id1",
                title : "意见反馈"
            },
            {
                image: "../../images/platform_Rule.png",
                listItem_id: "listItem_id2",
                title : "平台规则"
            },
            {
                image: "../../images/customerSevice.png",
                listItem_id: "listItem_id3",
                desc : "028-64312342",
                title: "联系客服"
            },
            {
                image: "../../images/listLocation.png",
                listItem_id: "listItem_id4",
                title: "地址管理"
            }
        ]
    },
    onTabbarItemTap:onTabbarItemTap,
    jumpMyOrderPage : function () {
        //wx.showActionSheet({
        //    itemList: ["涅米","fkasj"],
        //})
        wx.navigateTo({
            url: 'seeMinesOrder/seeMinesOrder?type=FALSE'
        })
    },
    onLoad : function (res) {
        Tools.requestBuyCarCount(false,this,4);
    },
    seeMinesOrder:function(e){
        let type =e.currentTarget.dataset.type;
        wx.navigateTo({
            url: 'seeMinesOrder/seeMinesOrder?type='+String(parseInt(type)-1)
        })
    },
    /**
     * 设置按钮点击跳转
     */
    settingClick : function() {
        wx.navigateTo({
            url: 'modifyPwdPage/modifyPwd'
        })

    },
    /**
     * 跳转用户基本信息
     */
    jumpUserBaseInfo : function() {
        wx:wx.navigateTo({
            url: 'mine_baseInfo/mineBaseInfo',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
        })
    },
    listItemClick : function (e) {
        var weakSelf =this;
        var curentIndex = e.currentTarget.id;
        if (curentIndex === "listItem_id1") {
            wx.navigateTo({
                url: 'suggest/suggest'
            })
        }
        if (curentIndex === "listItem_id2") {
            wx.navigateTo({
                url: 'rule/rule'
            })
        }
        if (curentIndex === "listItem_id4"){
            wx.navigateTo({
                url: 'addressManager/addressManager'
            })
        }
        if (curentIndex === "listItem_id3") {
            //联系客服
            wx.showModal({
                title: '确定拨打客服热线',
                content: '028-64312342',
                cancelText:"取消",
                cancelColor:"#0080FF",
                confirmColor:"#0080FF",
                confirmText:"拨打",
                success : function(res) {
                    if(res.confirm)
                    {
                        ///用户点击拨打
                        weakSelf.callSomePhone("028-64312342");
                    }
                }
            })
        }
    },
    middleMenusTap:function(item){
        const curentId = item.currentTarget.id;
        if(curentId == 'middleItem_Id1'){
            wx.navigateTo({
                url: 'coupon/coupon'
            })
        }
        if(curentId == 'middleItem_Id2'){
            wx.navigateTo({
                url: 'giveShoppingList/giveShoppingList'
            })
        }
        if(curentId == 'middleItem_Id3'){
            wx.navigateTo({
                url: 'balance/balance'
            })
        }
    },
    /**给某一个电话号码打电话 */
    callSomePhone : function (phoneNum) {
        wx.makePhoneCall({
            phoneNumber: phoneNum,
            success: function () {

            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    getAllInfo:function(){
        let This = this;
        requestMode.sendRequest('member/index','POST',{},{sKey:session.get('sKey')}).then(function(res){
            let code = res.data.code;
            if(code == 200){
                let data = res.data.data;
                This.setData({
                    userInfo:{
                        nickName:data.member_nickname,
                        avatar:data.member_face
                    },
                    middleMenus:[
                        {
                            image: "../../images/middleMenu_coupon.png",
                            itemName : "优惠券",
                            detail: data.member_coupon+"张",
                            middleItem_Id: "middleItem_Id1"
                        },
                        {
                            image: "../../images/middleMenu_sendProduct.png",
                            itemName: "满赠产品",
                            middleItem_Id: "middleItem_Id2"
                        },
                        {
                            image: "../../images/middleMenu_RestMoney.png",
                            itemName: "余额",
                            detail: "￥"+data.member_capital,
                            middleItem_Id: "middleItem_Id3"
                        }
                    ],
                    topMemus : [
                        {
                            image: "../../images/topMenu_waitPay.png",
                            title : "待付款",
                            topItem_Id:"topItem_Id1",
                            newBadge : data.not_pay,
                            type:1
                        },
                        {
                            image: "../../images/topMenu_waitSend.png",
                            title: "待发货",
                            topItem_Id: "topItem_Id2",
                            newBadge: 0,
                            type:2
                        },
                        {
                            image: "../../images/topMenu_waitRecieve.png",
                            title: "待收货",
                            topItem_Id: "topItem_Id3",
                            newBadge: 0,
                            type:3
                        },
                        {
                            image: "../../images/topMenu_hasDone.png",
                            title: "已完成",
                            topItem_Id: "topItem_Id4",
                            newBadge: 0,
                            type:4
                        },
                        {
                            image: "../../images/topMenu_exitOrder.png",
                            topItem_Id: "topItem_Id5",
                            title: "退单管理",
                            newBadge: 0,
                            type:5
                        }
                    ]
                })
            }else if(code == -2){
                wx.showModal({
                    title: '提示',
                    content: '登录已过期，是否重新登陆？',
                    success: function(res) {
                        if (res.confirm) {
                            This.wrongSkey();
                        }
                    }
                })
            }else{
                Tools.showErrorToast(res.data.message);
            }
        },function(res){
            Tools.showWrongNetWorkToast();
        })
    },
    wrongSkey:function(){
        let This = this;
        wx.login({
            success: function (loginResult) {
                wx.openSetting({
                    success: function (openSuccess) {
                        wx.getUserInfo({
                            success:function(res){
                                const header = {
                                    'code':loginResult.code,
                                    'iv':res.iv,
                                    'encrypted-data':res.encryptedData
                                };
                                session.set('iv',res.iv);
                                session.set('encrypted-data',res.encryptedData);
                                requestMode.sendRequest('login','POST',{},header).then(function(result){
                                    if(result.data.code == 200){
                                        session.set('sKey',result.data.data.sKey);
                                        wx.showModal({
                                            title: '提示',
                                            content: '登录成功，如页面无反应，请重启程序！'
                                        });
                                        This.onShow();
                                    }})
                            }
                        });
                    }
                });
            }
        });
    },
    onShow: function (options) {
        let This = this;
        This.getAllInfo();
    },
    onReady: function () {

    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {

    },
    onReachBottom: function () {

    },
    onShareAppMessage: function () {

    }
};
init(PageData);